package com.cqc.processmanagerdemo;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.format.Formatter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by cui on 2017/7/30.
 */

public class ProcessAdapter extends ArrayAdapter<ProcessInfo> {

    private List<ProcessInfo> userProList;
    private List<ProcessInfo> sysProList;


    public ProcessAdapter(Context context, List<ProcessInfo> userProList, List<ProcessInfo> sysProList) {
        super(context, 0);
        this.userProList = userProList;
        this.sysProList = sysProList;
    }

    @Override
    public int getCount() {
        return userProList.size() + sysProList.size() + 2;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0 || position == (userProList.size() + 1)) {
            return 0;
        } else {
            return 1;
        }
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        ViewHolder holder;
        int itemViewType = getItemViewType(position);
        if (itemViewType == 0) {
            convertView = setProcessType(position, convertView, parent);
        } else {
            convertView = setProcessInfo(position, convertView, parent);
        }
        return convertView;
    }

    private View setProcessInfo(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_process_info, parent, false);
            holder = new ViewHolder();
            holder.ivPro = (ImageView) convertView.findViewById(R.id.iv_icon);
            holder.tvProName = (TextView) convertView.findViewById(R.id.tv_pro_name);
            holder.tvMemSize = (TextView) convertView.findViewById(R.id.tv_mem_size);
            holder.cb = (CheckBox) convertView.findViewById(R.id.cb);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        ProcessInfo info;
        if (position > userProList.size()) {
            info = sysProList.get(position - 2 - userProList.size());
        } else {
            info = userProList.get(position - 1);
        }

//        holder.ivPro.setBackgroundDrawable(info.getIcon());//错误
        holder.ivPro.setImageDrawable(info.getIcon());
        holder.tvProName.setText(info.getName());
        holder.tvMemSize.setText(Formatter.formatShortFileSize(parent.getContext(), info.getMemorySize()));
        holder.cb.setChecked(info.isCheck());
        return convertView;
    }

    private View setProcessType(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_title, parent, false);
            holder = new ViewHolder();
            holder.tvProType = (TextView) convertView.findViewById(R.id.tv_pro_type);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (position == 0) {
            holder.tvProType.setText("用户进程数量:" + userProList.size());
        } else {
            holder.tvProType.setText("系统进程数量:" + sysProList.size());
        }
        return convertView;
    }

    static class ViewHolder {
        TextView tvProName;
        ImageView ivPro;
        TextView tvMemSize;
        CheckBox cb;
        TextView tvProType;
    }
}
